package com.dd.cloud.common.interceptor;


import com.dd.cloud.common.constants.HealthConstants;
import com.dd.cloud.common.enums.ZkExceptionEnum;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.utils.JsonUtils;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.vo.LoginInfoTransfer;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;

/**
 * SpringMVC拦截器，进行统一登录校验
 *
 * @author zk
 */
@Slf4j
@Data
public class UserInterceptor implements HandlerInterceptor {

//    private JwtUtils jwtUtils;
//    private JwtProperties prop;
//
//    public UserInterceptor(JwtProperties prop, JwtUtils jwtUtils) {
//        this.prop = prop;
//        this.jwtUtils = jwtUtils;
//    }

    /**
     * 缓存登录者信息,可以直接获取这个信息, 用type识别登陆者的身份
     */
    private static final ThreadLocal<LoginInfo> loginUserInfo = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String url = request.getRequestURL().toString();
        log.error("url={}", url);
        try {
//            String token = CookieUtils.getCookieValue(request, jwtUtils.getCookieName(request));
//            log.info("token:{}", token);
//            LoginInfo loginInfo = null;
//            if (!StringUtils.isBlank(token)) {
//                loginInfo = jwtUtils.getInfoFromToken(token);
//                loginUserInfo.set(loginInfo);
//            }
            String loginTransfer = request.getHeader(HealthConstants.CommonConstants.LOGIN_INFO);
            if (loginTransfer == null || StringUtils.isBlank(loginTransfer)) {
                throw new ZekeException(ZkExceptionEnum.LOGIN_AGAIN);
            }
            loginTransfer = URLDecoder.decode(loginTransfer, "utf-8");
            LoginInfoTransfer loginInfoTransfer = JsonUtils.strToObject(loginTransfer, LoginInfoTransfer.class);
            if (loginInfoTransfer == null || loginInfoTransfer == null) {
                throw new ZekeException(ZkExceptionEnum.LOGIN_AGAIN);
            }
            if (loginInfoTransfer.getAllowPath() && loginInfoTransfer.getLoginInfo() == null) {
                throw new ZekeException(ZkExceptionEnum.LOGIN_AGAIN);
            }
            loginUserInfo.set(loginInfoTransfer.getLoginInfo());
        } catch (ZekeException ex) {
            throw ex;
        } catch (Exception e) {
            log.error("身份解析失败！");
            e.printStackTrace();
            throw new ZekeException(ZkExceptionEnum.LOGIN_AGAIN);
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response,
                                Object handler,
                                Exception ex) {
        loginUserInfo.remove();
    }


    /**
     * 获取登录信息并判断登陆者
     *
     * @param userType
     * @return
     */
    public static ThreadLocal<LoginInfo> getLoginUserInfo(Integer userType) {
        if (loginUserInfo.get() == null) {
            throw new ZekeException(ZkExceptionEnum.NO_ERMSSION);
        }
        if (!loginUserInfo.get().getType().equals(userType)) {
            throw new ZekeException(ZkExceptionEnum.NO_ERMSSION);
        }
        return loginUserInfo;
    }

    /**
     * 获取登录信息
     *
     * @return
     */
    public static ThreadLocal<LoginInfo> getLoginUserInfo() {
        return loginUserInfo;
    }

}
